package com.mmall.controller.common;

import com.mmall.common.Const;
import com.mmall.pojo.User;
import com.mmall.util.CookieUtil;
import com.mmall.util.JsonUtil;
import com.mmall.util.RedisShardedPoolUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 用户登录过滤器(重置登录时间)
 */
public class SessionExpireFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        String loginToken = CookieUtil.readLoginToken((HttpServletRequest) servletRequest);
        if (StringUtils.isNotEmpty(loginToken)) {
            String userStr = RedisShardedPoolUtil.get(loginToken);
            User user = JsonUtil.strToObj(userStr, User.class);
            if (user != null) {
                RedisShardedPoolUtil.exPire(loginToken, Const.RedisCacheExtime.REDIS_SESSION_RXTIME);
            }

        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
